home *** CD-ROM | disk | FTP | other *** search
/ MacHack 2000 / MacHack 2000.toast / pc / The Hacks / Boxer / PalmBoxer / box.c next >
Encoding:
C/C++ Source or Header  |  2000-06-23  |  2.3 KB  |  136 lines

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <fcntl.h>
  5. #include <time.h>
  6.  
  7. #ifndef O_BINARY
  8. #define O_BINARY 0
  9. #endif
  10.  
  11. #ifndef __GNUC__
  12. unsigned long htonl(unsigned long x)
  13. {
  14.   unsigned char *cp, *dp;
  15.   unsigned long y;
  16.   dp = &y;
  17.   cp = &x;
  18.   dp += 3;
  19.   *dp-- = *cp++;
  20.   *dp-- = *cp++;
  21.   *dp-- = *cp++;
  22.   *dp-- = *cp++;
  23.   return y;
  24. }
  25.  
  26. unsigned short htons(unsigned short x)
  27. {
  28.   unsigned char *cp, *dp;
  29.   unsigned short y;
  30.   dp = &y;
  31.   cp = &x;
  32.   dp += 1;
  33.   *dp-- = *cp++;
  34.   *dp-- = *cp++;
  35.   return y;
  36. }
  37. #else
  38. #include <unistd.h>
  39. #include <sys/types.h>
  40. #include <netinet/in.h>
  41. #endif
  42.  
  43. int main(int argc, char *argv[])
  44. {
  45.  
  46.   int outfd;
  47.   unsigned long cofst;
  48.   unsigned char *bbuf, nbuf[32];
  49.   unsigned short xshort;
  50.   unsigned long xlong, xid;
  51.   long bbuflen;
  52.   int fd, maxsect;
  53.  
  54.   if (argc != 2) {
  55.     fprintf(stderr, "Usage: %s inputfile\n", argv[0]);
  56.     exit(1);
  57.   }
  58.  
  59.   bbuf = malloc(8 + 4096);
  60.  
  61.   strcpy(bbuf, argv[1]);
  62.   strcat(bbuf, ".pdb");
  63.   if (!(outfd = open(bbuf, O_BINARY | O_WRONLY | O_CREAT | O_TRUNC, 0644)))
  64.     exit(-1);
  65.  
  66.   strcpy(bbuf, "DBLK");
  67.   bbuf[5] = 0;
  68.   bbuf[6] = 0x10;
  69.   bbuf[7] = 0;
  70.  
  71.   memset(nbuf, 0, 32);
  72.   strncpy(nbuf, argv[1], 32);
  73.   write(outfd, nbuf, 32);
  74.  
  75.   memset(nbuf, 0, 4);
  76.   nbuf[1] = 0x88;
  77.   nbuf[3] = 1;
  78.   write(outfd, nbuf, 4);
  79.  
  80.   xlong = time(NULL) + ((66 * 365 + 17) * 24 * 3600UL);
  81.   xlong = htonl(xlong);
  82.   write(outfd, &xlong, 4);
  83.   write(outfd, &xlong, 4);
  84.   write(outfd, &xlong, 4);
  85.  
  86.   memset(nbuf, 0, 12);
  87.   write(outfd, nbuf, 12);
  88.  
  89.   write(outfd, "DATA", 4);
  90.   write(outfd, "BRWS", 4);
  91.  
  92.   nbuf[0] = 0x28;
  93.   write(outfd, nbuf, 8);
  94.  
  95.   fd = open(argv[1], O_BINARY | O_RDONLY);
  96.   xlong = lseek(fd, 0, SEEK_END);
  97.   lseek(fd, 0, SEEK_SET);
  98.  
  99.   maxsect = xlong / 4096 + 1;
  100.   xshort = htons(maxsect);
  101.   write(outfd, &xshort, 2);
  102.  
  103.   cofst = 80 + 8 * maxsect;
  104.  
  105.   xid = 0x40000000UL + (rand() & 0x7fffffUL);
  106.   for (;;) {
  107.  
  108.     bbuflen = read(fd, &bbuf[8], 4096);
  109.  
  110.     if (bbuflen <= 0)
  111.       break;
  112.  
  113.     xlong = htonl(cofst);
  114.     write(outfd, &xlong, 4);
  115.     xlong = htonl(xid++);
  116.     write(outfd, &xlong, 4);
  117.  
  118.     xlong = lseek(outfd, 0, SEEK_CUR);
  119.     lseek(outfd, cofst, SEEK_SET);
  120.     write(outfd, bbuf, bbuflen + 8);
  121.     lseek(outfd, xlong, SEEK_SET);
  122.     cofst += bbuflen + 8;
  123.  
  124.     if (bbuflen != 4096)
  125.       break;
  126.   }
  127.  
  128.   close(fd);
  129.  
  130.  
  131.   xshort = 0;
  132.   write(outfd, &xshort, 2);
  133.  
  134.   return 0;
  135. }
  136.